foodCtrl.$inject = ['foodService','merchantService','$location','$rootScope','$stateParams','$uibModal'];

function foodCtrl(foodService,merchantService,$location,$rootScope,$stateParams,$uibModal) {

  var vm=this;
 //sessionStorageDemo 
  // sessionStorage.removeItem("client");
  // if(null!=sessionStorage.client){
  //   var clientInfo = JSON.parse(sessionStorage.client); 
  // }
  
  // console.log(clientInfo);
  // if(!sessionStorage.client){
  //   $location.path("/login");
  //   return;
  // }
  

  var icnum = $stateParams.icnum;
  // sessionStorage.icnum = icnum;

  foodService.findFoodByIcnum(icnum,function(res) {
      vm.foodList = res.data;
      // sessionStorage.removeItem("merchant")
      if(!sessionStorage.merchant){
        merchantService.findMerchantByIcnum(icnum,function(res){
          sessionStorage.merchant = JSON.stringify(res.data);
          vm.merchant = res.data;
         })
      }else{
        if(sessionStorage.merchant.icnum != icnum){
          merchantService.findMerchantByIcnum(icnum,function(res){
            sessionStorage.merchant = JSON.stringify(res.data);
            vm.merchant = res.data;
           })
        }else{
          vm.merchant = JSON.parse(sessionStorage.merchant);
        }
        
      }
      
      
  })
  vm.getFoodByIcnumFtype = function(ftype) {        
    // $location.path("/food/"+icnum);
    foodService.getFoodByIcnumFtype(icnum,ftype,function(res){
      vm.foodList = res.data;
    });
  },
  vm.addToCart = function(food) {
    //不同商家添加购物车之前需要清空购物车
    if(sessionStorage.flag!='true'){
      $location.path('/login');
    }else
    {var sesStorageIcnum = sessionStorage.icnum;
    if(icnum!=sesStorageIcnum){
      sessionStorage.removeItem("cartInfo");
    }
    //更新商家icnum
    sessionStorage.icnum = icnum;

    // sessionStorage.removeItem("cartInfo");
    if(null!=sessionStorage.cartInfo){
      var cartInfo = JSON.parse(sessionStorage.cartInfo);
      var flag = false; 
      for(let i = 0;i<cartInfo.length;i++){
        if(cartInfo[i].food._id == food._id){
          cartInfo[i].count += 1;
          flag =  true;
        }
      }
      if(!flag){
        cartInfo.push({'food':food,'count':1});
      }
      console.log(cartInfo);
      sessionStorage.cartInfo = JSON.stringify(cartInfo);
    }else{
      let foodInfo = {'food':food,'count':1};
      let foodInfos = [];
      foodInfos.push(foodInfo);
      console.log(foodInfos);
      sessionStorage.cartInfo = JSON.stringify(foodInfos);
    }

    //模态框
    var modalInstace = $uibModal.open({
      template: require('./foodModalTemplate.html'),
      ariaLabelledBy: 'modal-title',
      controller:'foodModalCtrl',
      controllerAs: 'vm'   
    });

    modalInstace.result.then(function (item) {
        // vm.addrlist.push(item);
        //模态框消失以后显示所有deliverInfo
        // deliverInfoSvc.getDeliverInfoByClient($scope.client._id, function(err, addrlist) {
        //     vm.addrlist = addrlist;
        // })
    })}

}

  }



module.exports = foodCtrl;